package com.cm.sys.intermanager.service.impl;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.client.RestTemplate;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.cm.sys.framework.application.ApiResponseCode;
import com.cm.sys.framework.execption.BusinessException;
import com.cm.sys.intermanager.service.LoggerService;
import com.cm.sys.intermanager.utils.HttpResult;

@Service
public class LoggerServiceImpl implements LoggerService {

    private static Logger logger = LoggerFactory.getLogger(LoggerServiceImpl.class);
    @Autowired
    private RestTemplate restTemplate;

    /***
     * 业务处理层的ip端口
     */
    @Value("${ServiceUrl}")
    private String ServiceUrl;

    /***
     * 按查询时间查询
     */
    @Value("${LoggerTime}")
    private String loggerTime;

    /***
     * 按手机查询
     */
    @Value("${LoggerPhone}")
    private String loggerPhone;

    /***
     * 按流水查询
     */
    @Value("${LoggerTransId}")
    private String loggerTransId;

    /***
     * 主机流水加时间
     */
    @Value("${LoggerPhoneTransId}")
    private String loggerPhoneTransId;

    /***
     * 按手机查询没有时间
     */
    @Value("${LoggerPhoneNoTime}")
    private String loggerPhoneNoTime;

    /***
     * 按流水查询没有时间
     */
    @Value("${LoggerTransIdNoTime}")
    private String loggerTransIdNoTime;

    /***
     * 主机流水没有时间
     */
    @Value("${LoggerPhoneTransIdNoTime}")
    private String loggerPhoneTransIdNoTime;

    @Override
    public HttpResult getLoggerTime(JSONObject json) {

        if (StringUtils.isEmpty(ServiceUrl) || StringUtils.isEmpty(loggerTime)) {
            throw new BusinessException(ApiResponseCode.PARAM_NEED.getCode(), ApiResponseCode.PARAM_NEED.getMessage());
        }

        logger.info("=======按时间查询的日志请求路径========{}===============", ServiceUrl + loggerTime);

        HttpResult reponse = restTemplate.postForObject(ServiceUrl + loggerTime, json, HttpResult.class);

        logger.info("=======按时间查询的返回日志信息========{}===============",
            StringUtils.isEmpty(reponse) ? "暂无数据" : JSON.toJSON(reponse));

        return reponse;
    }

    @Override
    public HttpResult getLoggerPhone(JSONObject json) {
        if (StringUtils.isEmpty(ServiceUrl) || StringUtils.isEmpty(loggerPhone)) {
            throw new BusinessException(ApiResponseCode.PARAM_NEED.getCode(), ApiResponseCode.PARAM_NEED.getMessage());
        }

        logger.info("=======按手机号码查询的日志请求路径========{}===============", ServiceUrl + loggerPhone);

        HttpResult http = restTemplate.postForObject(ServiceUrl + loggerPhone, json, HttpResult.class);

        logger.info("=======按手机号码查询的日志返回信息========{}===============",
            StringUtils.isEmpty(http) ? "暂无数据" : JSON.toJSON(http));

        return http;
    }

    @Override
    public HttpResult getLoggerTransId(JSONObject json) {
        if (StringUtils.isEmpty(ServiceUrl) || StringUtils.isEmpty(loggerTransId)) {
            throw new BusinessException(ApiResponseCode.PARAM_NEED.getCode(), ApiResponseCode.PARAM_NEED.getMessage());
        }

        logger.info("=======按流水单号查询的日志请求路径========{}===============", ServiceUrl + loggerTransId);
        HttpResult http = restTemplate.postForObject(ServiceUrl + loggerTransId, json, HttpResult.class);

        logger.info("=======按流水单号查询的返回的日志信息========{}===============",
            StringUtils.isEmpty(http) ? "暂无数据" : JSON.toJSON(http));

        return http;
    }

    @Override
    public HttpResult getLoggerPhoneTransId(JSONObject json) {

        if (StringUtils.isEmpty(ServiceUrl) || StringUtils.isEmpty(loggerPhoneTransId)) {
            throw new BusinessException(ApiResponseCode.PARAM_NEED.getCode(), ApiResponseCode.PARAM_NEED.getMessage());
        }

        logger.info("=======按流水单号和手机号码查询的日志请求路径========{}===============", ServiceUrl + loggerTransId);
        HttpResult http = restTemplate.postForObject(ServiceUrl + loggerPhoneTransId, json, HttpResult.class);

        logger.info("=======按流水单号和手机号码查询的返回的日志信息========{}===============",
            StringUtils.isEmpty(http) ? "暂无数据" : JSON.toJSON(http));

        return http;
    }

    @Override
    public HttpResult getLoggerPhoneNoTime(JSONObject json) {
        if (StringUtils.isEmpty(ServiceUrl) || StringUtils.isEmpty(loggerPhone)) {
            throw new BusinessException(ApiResponseCode.PARAM_NEED.getCode(), ApiResponseCode.PARAM_NEED.getMessage());
        }

        logger.info("=======按手机号码没有时间查询的日志请求路径========{}===============", ServiceUrl + loggerPhoneNoTime);

        HttpResult http = restTemplate.postForObject(ServiceUrl + loggerPhoneNoTime, json, HttpResult.class);

        logger.info("=======按手机号码没有时间查询的日志返回信息========{}===============",
            StringUtils.isEmpty(http) ? "暂无数据" : JSON.toJSON(http));

        return http;
    }

    @Override
    public HttpResult getLoggerTransIdNoTime(JSONObject json) {
        if (StringUtils.isEmpty(ServiceUrl) || StringUtils.isEmpty(loggerTransId)) {
            throw new BusinessException(ApiResponseCode.PARAM_NEED.getCode(), ApiResponseCode.PARAM_NEED.getMessage());
        }

        logger.info("=======按流水单号没有时间查询的日志请求路径========{}===============", ServiceUrl + loggerTransIdNoTime);
        HttpResult http = restTemplate.postForObject(ServiceUrl + loggerTransIdNoTime, json, HttpResult.class);

        logger.info("=======按流水单号没有时间查询的返回的日志信息========{}===============",
            StringUtils.isEmpty(http) ? "暂无数据" : JSON.toJSON(http));

        return http;
    }

    @Override
    public HttpResult getLoggerPhoneTransIdNoTime(JSONObject json) {
        if (StringUtils.isEmpty(ServiceUrl) || StringUtils.isEmpty(loggerPhoneTransId)) {
            throw new BusinessException(ApiResponseCode.PARAM_NEED.getCode(), ApiResponseCode.PARAM_NEED.getMessage());
        }

        logger.info("=======按流水单号和手机号码没有时间查询的日志请求路径========{}===============", ServiceUrl + loggerPhoneTransIdNoTime);
        HttpResult http = restTemplate.postForObject(ServiceUrl + loggerPhoneTransIdNoTime, json, HttpResult.class);

        logger.info("=======按流水单号和手机号码查询没有时间的返回的日志信息========{}===============",
            StringUtils.isEmpty(http) ? "暂无数据" : JSON.toJSON(http));

        return http;
    }

}
